﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SpriteMaker;
using System.IO;

namespace UIMaker
{
    
    public partial class FrameBrowser : Form
    {
        public static List<LoadedSprite> loadedSprites = new List<LoadedSprite>();
        private LoadedSprite sprite = null;
        public static Sprite lastUsedSprite;
        public SelectedFrame frame = new SelectedFrame();
        private FramePreview previewPannel;
        public FrameBrowser()
        {
            InitializeComponent();
            previewPannel = new FramePreview();
            splitFrameBrowser.Panel2.Controls.Add(previewPannel);
            foreach (Sprite sprite in loadedSprites)
            {
                comboBoxSprite.Items.Add(sprite.spriteName);
            }
            comboBoxSprite.Items.Add("<Import sprite>");
            listViewFrame.Columns.Add("ID");
            listViewFrame.Columns.Add("Description"); 
        }

        private void comboBoxSprite_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxSprite.SelectedItem == "<Import sprite>")
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Filter = "Sprite (*.sprite)|*.sprite";
                dialog.CheckFileExists = true;
                dialog.ShowDialog();
                if (dialog.FileName != "")
                {
                    String name = Path.GetFileNameWithoutExtension(dialog.FileName);
                    foreach (LoadedSprite s in loadedSprites)
                    {
                        if (s.spriteName.Equals(name, StringComparison.CurrentCultureIgnoreCase))
                        {
                            sprite = s;
                            break;
                        }
                    }
                    if (sprite == null)
                    {
                        sprite = new LoadedSprite();
                        TextReader reader = new StreamReader(dialog.FileName, Encoding.ASCII);
                        String data = reader.ReadToEnd();
                        reader.Close();
                        sprite.FromStringXML(data);
                        sprite.spriteName = name;
                        String spritePath = System.IO.Path.GetDirectoryName(dialog.FileName) + "\\";
                        if (File.Exists(spritePath + sprite.imageName))
                        {
                            sprite.image = LoadImage(spritePath + sprite.imageName);
                        }
                        else
                        {
                            MessageBox.Show(sprite.imageName + " not found");
                        }
                        loadedSprites.Add(sprite);
                        comboBoxSprite.Items.Insert(0, sprite.spriteName);
                        comboBoxSprite.SelectedIndex = 0;
                        return;
                    }
                }
            }
            else
            {
                foreach (LoadedSprite s in loadedSprites)
                {
                    if (s.spriteName.Equals(comboBoxSprite.SelectedItem.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        sprite = s;
                        break;
                    }
                }
            }
            if (sprite == null) return;
            foreach (Frame frame in sprite.frameList)
            {
                ListViewItemFrame item = new ListViewItemFrame(frame.id.ToString());
                item.frame = frame;
                if (frame.description != "")
                {
                    item.SubItems.Add(frame.description);
                }
                listViewFrame.Items.Add(item);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            frame = null;
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listViewFrame_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewFrame.SelectedItems.Count > 0)
            {
                frame.frame = ((ListViewItemFrame)listViewFrame.SelectedItems[0]).frame;
                frame.sprite = sprite;
                previewPannel.SetActiveFrame(frame.frame);
                previewPannel.SetActiveImage(sprite.image);
                previewPannel.Invalidate();
            }
        }
        public Image LoadImage(String path)
        {
            Image image;
            String extension = System.IO.Path.GetExtension(path);
            if (String.Compare(extension, ".dds", true) == 0)
            {
                byte[] data = File.ReadAllBytes(path);
                image = Util.DDSToBitmap(data);
            }
            else
            {
                image = Image.FromFile(path);
            }
            return image;
        }
    }
    public class ListViewItemFrame : ListViewItem
    {
        public Frame frame;
        public ListViewItemFrame(String text)
            : base(text)
        {
        }
    }
    public class LoadedSprite : Sprite
    {
        public Image image;
    }
}
